﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace SMS
{
    public partial class ComposeMessage : Form
    {
		#region Fields (7) 

        private List<String> mlContacts;
        private int mMsgMode;
        private String msmsContent;
        private String mwapContent;
        private String mwapUrl;
        public const int SMS_TYPE_TEXT = 0;
        public const int SMS_TYPE_WAPPUSH = 1;

		#endregion Fields 

		#region Constructors (1) 

        public ComposeMessage()
        {
            InitializeComponent();
            changeMode(SMS_TYPE_TEXT);
            mlContacts = new List<String>();
            mMsgMode = SMS_TYPE_TEXT;
        }

		#endregion Constructors 

		#region Properties (5) 

        public List<String> Contacts
        {
            get { return mlContacts; }
        }

        public int MsgMode
        {
            get { return mMsgMode; }
            set { mMsgMode = value; }
        }

        public String SmsContent
        {
            get { return msmsContent; }
            set { msmsContent = value; }
        }

        public String WapContent
        {
            get { return mwapContent; }
            set { mwapContent = value; }
        }

        public String WapUrl
        {
            get { return mwapUrl; }
            set { mwapUrl = value; }
        }

		#endregion Properties 

		#region Methods (10) 

		// Public Methods (1) 

        public List<SMSContent> getSMSContentList()
        {
            List<SMSContent> contentList = new List<SMSContent>();
            foreach (string contact in Contacts)
            {
                if (!isValidContact(contact)) continue;
                SMSContent content = new SMSContent();

                // check if number start with 0, remove 0 and add +84
                if (contact[0] == '0')
                    content.Destination = "+84" + contact.Substring(1);
                content.Destination = contact;
                if (MsgMode == ComposeMessage.SMS_TYPE_TEXT)
                {
                    content.Type = Properties.Resources.sms_small;
                    content.Content = SmsContent;
                    content.URL = "";
                }
                else if (MsgMode == ComposeMessage.SMS_TYPE_WAPPUSH)
        {
                    content.Type = Properties.Resources.wap_icon;
                    content.Content = WapContent;
                    content.URL = WapUrl;
                }
                content.CreatedDate = DateTime.Now.ToString();
                contentList.Add(content);
            }
            return contentList;
        }
		// Private Methods (9) 

        private void btCancel_Click(object sender, EventArgs e)
        {
            rbTextSMS.Checked = true;
            rbWapPush.Checked = false;
            SmsContent = "";
            WapContent = "";
            WapUrl = "";
            this.Close();
        }

        private void btCreate_Click(object sender, EventArgs e)
        {
            if (MsgMode == SMS_TYPE_TEXT)
            {
                SmsContent = tbTextMsg.Text;
                WapContent = "";
                WapUrl = "";
            }
            if (MsgMode == SMS_TYPE_WAPPUSH)
            {
                // check wappush url
                if (tbWPUrl.Text == "")
        {
                    MessageBox.Show("Nhập đường dẫn của tin wappush!");
                    return;
                }
                SmsContent = "";
                WapContent = tbWPContent.Text;
                WapUrl = tbWPUrl.Text;
        }
            foreach (String contact in tbContactList.Lines)
        {
                Contacts.Add(contact);
            }
            this.Close();
        }

        private void btImport_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Text Files|*.txt";
            //openFileDialog1.Filter = "Text Files|*.txt";//|Excel Files|*.xls";
            openFileDialog1.Title = "Chọn file để lấy danh sách số";
            openFileDialog1.Multiselect = true;
            openFileDialog1.FileName = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string extension = "";
                foreach (string filename in openFileDialog1.FileNames)
                {
                    extension = System.IO.Path.GetExtension(filename);
                    if (extension.CompareTo(".txt") == 0)
                        importContactFromTextFile(filename);
                    else if (System.IO.Path.GetExtension(filename) == ".xls")
                        importContactFromExcelFile(filename);
                    else
                        MessageBox.Show("Hiện tại không hỗ trợ kiểu file " + extension);
                }
            }


            //String MusicFile = "";
            //MusicFile = openFileDialog1.FileNames[];
        }

        private void changeMode(int mode)
        {
            mMsgMode = mode;
            tbTextMsg.Text = "";
            tbWPContent.Text = "";
            
            if (mode == SMS_TYPE_TEXT)
            {
                tbTextMsg.Text = "";
                tbTextMsg.Enabled = true;
                rbWapPush.Checked = false;
                tbWPContent.Enabled = false;
                tbWPUrl.Enabled = false;
                tbWPUrl.Text = "Nhập đường dẫn vào đây";
            }
            else if (mode == SMS_TYPE_WAPPUSH)
            {
                rbTextSMS.Checked = false;
                tbTextMsg.Enabled = false;
                tbWPContent.Enabled = true;
                tbWPUrl.Enabled = true;
                tbWPUrl.Text = "";
            }

        }

        // function: importContactFromExcelFile
        // description: read contact from the first colum in the first excel sheet.
        // @param: string filename filename to read
        private void importContactFromExcelFile(string filename)
        {
            // TODO: implement
        }

        // function: importContactFromTextFile
        // description: read contact from text file, each contact is one line
        // @param: string filename filename to read
        private void importContactFromTextFile(string filename)
            {
            using (StreamReader sr = new StreamReader(filename))
            {
                try
                {
                    
                    tbContactList.Text =  sr.ReadToEnd();
                    return;

                    /*
                    string line;
                    line = sr.ReadLine();
                    tbContactList.AppendText( line);
                    while ((line = sr.ReadLine()) != null)
                    {
                        tbContactList.AppendText("\n" + line);
                    }
                     * */

                }
                catch (IOException e)
                {
                    MessageBox.Show("Có lỗi trong quá trình đọc file");
                }
            }
        }

        private bool isValidContact(string contact)
        {
            string allowCharacter = "+0123456789";
            if (contact.Length < 10 || contact.Length > 13) return false;
            // check for vietnam international number format +84
            if (contact[0] == '+')
                contact = contact.Substring(1);
            
            foreach (char a in contact)
            {
                if (allowCharacter.Contains(a.ToString()) == false)
                    return false;
            }
            return true;
        }

        private void rbTextSMS_Checked(object sender, EventArgs e)
        {
            changeMode(SMS_TYPE_TEXT);
        }

        private void rbWapPush_Checked(object sender, EventArgs e)
        {
            changeMode(SMS_TYPE_WAPPUSH);
        }

		#endregion Methods 
    }

}
